home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_12_10
/
allison
/
arglist.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1994-09-06
|
1KB
|
59 lines
LISTING 13
#include <fstream.h>
#include <string.h>
#include "arglist.h"
Arglist::Arglist(size_t arg_count, char **arg_vec)
{
args = new char*[arg_count];
available = arg_count;
used = 0;
// INVARIANT: available+used == amount allocated
for (int i = 0; i < arg_count; ++i)
if (arg_vec[i][0] == '@')
expand(arg_vec[i]+1);
else
add(arg_vec[i]);
}
Arglist::~Arglist()
{
for (int i = 0; i < used; ++i)
delete [] args[i];
delete [] args;
}
void Arglist::expand(char *fname)
{
ifstream f(fname);
const size_t BUFSIZ = 64;
char token[BUFSIZ];
while (f >> token)
if (token[0] == '@')
expand(token+1);
else
add(token);
}
void Arglist::add(char *arg)
{
if (!available)
{
/* Grow argument list */
char **new_args = new char*[used + CHUNK];
for (int i = 0; i < used; ++i)
new_args[i] = args[i];
delete [] args;
args = new_args;
available = CHUNK;
}
/* Allocate space for and store argument */
args[used] = new char[strlen(arg) + 1];
strcpy(args[used++], arg);
--available;
}